<?php

class StudyController extends Controller
{


    /*
    * 获取列表分页
    *
    */
    public function actionGetListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();


            //获取count
            $totalcount = StudyInfolist ::model()->count($searchsql);

                //排序
                $searchsql = $searchsql . " order by create_time  desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list = StudyInfolist::model()->findAll($searchsql);

                for($i=0;$i<count($list);$i++)
                {
                    $obj = $list[$i];

                    if(StudyService::isStarBefore($obj->id,$user_id)==true)
                    {
                        $obj->mystarcount = 1;
                    }

                }


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
    * 添加评论
    *
    */
    public function actionComment()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $study_id = Util::getParamNotEmpty("study_id");
            $comment = Util::getParamNotEmpty("comment");

            $parentcommentid = Util::getParamNotEmpty("parentcommentid");


            $study = StudyInfolist::model()->findByPK($study_id);
            Util::checkExistAndNotDelete($study);


            //事务开始
            $connection = Yii::app()->db;

            $transaction = $connection->beginTransaction();

            try {


                if($parentcommentid != "-1")
                {
                    $commnet = StudyComment::model()->findByPK($parentcommentid);
                    Util::checkExistAndNotDelete($commnet);
                }



                //添加
                $obj = new StudyComment();
                Util::initObjData($obj);

                $obj->study_id = $study->id;
                $obj->user_id = $user_id;
                $obj->content = $comment;
                $obj->parentcomment_id = $parentcommentid;
                Util::saveObjData($obj);

                //评论数＋1

                $study->comment_count +=1;

                Util::updateObjData($study);
                Util::saveObjData($study);


                $transaction->commit();//事务结束
            } catch (Exception $e) {
                $transaction->rollback();//回滚函数

                throw new Exception($e->getMessage());

            }



            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 获取评论列表分页
    *
    */
    public function actioncommentlist()
    {

        try {
            //参数验证

            $study_id = Util::getParamNotEmpty("study_id");

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStr($searchsql,"study_id",$study_id);


            //获取count
            $totalcount = StudyComment ::model()->count($searchsql);

                //排序
            $searchsql = $searchsql . " order by create_time  desc";

                //加上分页控制
            $searchsql = Util::addLimitSql($searchsql);

                //获取list
            $list = StudyComment ::model()->findAll($searchsql);

            $detailarray = array();

            for($i=0;$i<count($list);$i++)
            {
                $obj = $list[$i];

                $user = UcUser::model()->findByPK($obj->user_id);

                $detailarray["user"] = UserService::formatUser($user) ;

                if($obj->parentcomment_id >0)
                {
                    $pcom = StudyComment::model()->findByPK($obj->id);

                    if(empty($pcom)==false) {

                        $puser = UcUser::model()->findByPK($pcom->user_id);

                        $detailarray["puser"] = UserService::formatUser($puser) ;



                    }

                }

                $obj->text_str = $detailarray;




            }


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
   * 点赞
   *
   */
    public function actionaddstar()
    {

        //事务开始
        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $study_id = Util::getParamNotEmpty("study_id");




            $study = StudyInfolist::model()->findByPK($study_id);
            Util::checkExistAndNotDelete($study);


            //是否已经点赞
            if(StudyService::isStarBefore($study_id,$user_id)==true)
                throw new Exception("已经点过赞了");



            $obj = StudyStar::model()->find("study_id='".$study_id."' and user_id='".$user_id."' order by create_time desc");


            if(empty($obj)==true)
            {
                //添加
                $obj = new StudyStar();
                Util::initObjData($obj);

            }
            else
            {
                Util::recoverObjData($obj);
            }


                $obj->study_id = $study->id;
                $obj->user_id = $user_id;
                $obj->is_active = 1;

                Util::saveObjData($obj);

                //评论数＋1

                $study->star_count +=1;

                Util::updateObjData($study);
                Util::saveObjData($study);


                $transaction->commit();//事务结束

                Util::responseMsg(MsgConst::RETURN_SUCCESS);






        } catch (Exception $e) {


            $transaction->rollback();//回滚函数

            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
  * 取消点赞
  *
  */
    public function actioncancelstar()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $study_id = Util::getParamNotEmpty("study_id");




            $study = StudyInfolist::model()->findByPK($study_id);
            Util::checkExistAndNotDelete($study);


            //是否已经点赞
            if(StudyService::isStarBefore($study_id,$user_id)==false)
                throw new Exception("之前并未点过赞了");

            //return;

            //事务开始
            $connection = Yii::app()->db;

            $transaction = $connection->beginTransaction();

            try {





                //添加
                $obj = StudyService::getStarObj($study_id,$user_id);
                Util::deleteObjData($obj);

                Util::saveObjData($obj);

                //评论数＋1

                $study->star_count -=1;

                Util::updateObjData($study);
                Util::saveObjData($study);


                $transaction->commit();//事务结束
            } catch (Exception $e) {
                $transaction->rollback();//回滚函数

                throw new Exception($e->getMessage());

            }



            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


}